package com.qk.management.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Course;
import com.qk.management.aop.anno.LogAnno;
import com.qk.management.service.CourseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author 李智勇
 * @data 2025/10/5 14:46
 */
@Slf4j
@RestController
@RequestMapping("/courses")
public class CourseController {
    @Autowired
    private CourseService courseService;
    @GetMapping("/subject/{subjectId}")
    public Result listBySubject(@PathVariable Integer subjectId){
        return Result.success(courseService.listBySubject(subjectId));
    }
    @GetMapping("/list")
    public Result list(){
        return Result.success(courseService.list());
    }
    @LogAnno
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Integer id){
        log.info("删除课程id:{}",id);
        courseService.deleteById(id);
        return Result.success();
    }
    @LogAnno
    @PutMapping
    public Result update(@RequestBody Course course){
        log.info("修改课程:{}",course);
        courseService.update(course);
        return Result.success();
    }
    @GetMapping("/{id}")
    public Result getById(@PathVariable("id") Integer id) {
        log.info("查询课程id:{}",id);
        Course course = courseService.getById(id);
        return Result.success(course);
    }
    @GetMapping
    public Result page(
            @RequestParam(name = "subject", required = false) Integer subject,
            @RequestParam(name = "name", required = false) String name,
            @RequestParam(name = "target", required = false) Integer target,
            @RequestParam(name = "page",  defaultValue = "1") Integer page,
            @RequestParam(name = "pageSize",  defaultValue = "10") Integer pageSize)
    {
        PageResult<Course> pageResult = courseService.page(subject, name, target, page, pageSize);
        log.info("分页查询课程,参数:课程主题={},课程名称={},课程目标={},当前页码={},每页大小={}", subject, name, target, page, pageSize);
        return Result.success(pageResult);
    }
    @LogAnno
    @PostMapping
    public Result addCourse(@RequestBody Course course){
        log.info("添加课程：{}", course);
        courseService.addCourse(course);
        return Result.success();
    }
}